package cbbx_sm.decision_maker;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import cbbx_sm.probabilistic_model.Cluster;
import cbbx_sm.probabilistic_model.Prediction;

public class RoundRobinDecisionMaker implements IDecisionMaker{
	private HashMap<String, Integer> curCameraIndex = new HashMap<String, Integer>();
	private HashMap<String, List<Cluster>> map;
	private boolean goUP;
	
	public RoundRobinDecisionMaker(List<Cluster> clusters, boolean goUP){
		map = getCameraClusters(clusters);
		for (String cameraId : map.keySet())
			curCameraIndex.put(cameraId, 0);
		this.goUP = goUP;
	}
	
	@Override
	public Decision makeDecision(Prediction currentPrediction){		
		List<CameraConfiguration> confs = new Vector<CameraConfiguration>();
		
		for (String cameraId : map.keySet()){
			int curIndex = curCameraIndex.get(cameraId);
			if (curIndex >= map.get(cameraId).size()){
				curCameraIndex.put(cameraId, 0);
				curIndex = curCameraIndex.get(cameraId);
				if (goUP){
					continue;
				}
			}
						
			Cluster choosenCluster = map.get(cameraId).get(curIndex);
			confs.add(new CameraConfiguration(cameraId, choosenCluster));
			curCameraIndex.put(cameraId, curIndex + 1);
		}
		
		Decision decision = new Decision(confs);
		return decision;
	}
	
	private HashMap<String, List<Cluster>> getCameraClusters(List<Cluster> fusedClusters){
		HashMap<String, List<Cluster>> map = new HashMap<String, List<Cluster>>();
		for (Cluster c : fusedClusters){
			if (!map.containsKey(c.getCameraId()))
				map.put(c.getCameraId(), new Vector<Cluster>());
			map.get(c.getCameraId()).add(c);
		}
		
		return map;
	}
	
	@Override
	public String toString(){
		return "" + this.getClass().getSimpleName()+(goUP?"GoUP":"CycleZoom");
	}
}
